package com.jianwei.web.service;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.jianwei.web.dao.UserDao;
import com.jianwei.web.pojo.Users;
import com.jianwei.web.util.StrutsUtils;

@Component
@Transactional
public class UserService {
	@Autowired
	private UserDao userDao;
	
	/**
	 * 生成一段json串
	 * @return
	 */
	public String readJson(){
		Map<String, String> map = new HashMap<String, String>();
		map.put("Test", "Json");
		map.put("Read", "Json");
		return JSONArray.fromObject(map).toString();
	}
	
	/**
	 * 添加用户
	 * @param user
	 */
	public void addUser(Users user){
		userDao.save(user);
	}
	
	/**
	 * 查询所有用户
	 * @return 将所有用户信息返回
	 */
	public String findAllUsers(){
		return JSONArray.fromObject(userDao.findAll()).toString();
	}
	
	/**
	 * 用户登出
	 * @return 清除application 与session 
	 */
	public void userLogoff(){
		Users user = new Users();
		user = (Users)StrutsUtils.getSession().getAttribute("user");
		StrutsUtils.getSession().getServletContext().removeAttribute(user.getUser_name());
		StrutsUtils.getSession().removeAttribute("user");
	}
}
